package com.example.shutdown_v1.controller;

import com.example.shutdown_v1.constants.PowerDeviceConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * Redis数据清理控制器
 */
@RestController
@RequestMapping("/api/redis")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin(originPatterns = "*")
public class RedisCleanupController {
    
    private final RedisTemplate<String, Object> redisTemplate;
    
    /**
     * 清理所有电力设备相关的Redis数据
     */
    @DeleteMapping("/cleanup/power")
    public ResponseEntity<Map<String, Object>> cleanupPowerData() {
        try {
            int deletedCount = 0;
            
            // 清理查询记录
            Set<String> queryKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.POWER_QUERY_PREFIX + "*");
            if (queryKeys != null && !queryKeys.isEmpty()) {
                deletedCount += redisTemplate.delete(queryKeys).intValue();
                log.info("[Redis清理] 删除查询记录 {} 条", queryKeys.size());
            }
            
            // 清理响应记录
            Set<String> responseKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.POWER_RESPONSE_PREFIX + "*");
            if (responseKeys != null && !responseKeys.isEmpty()) {
                deletedCount += redisTemplate.delete(responseKeys).intValue();
                log.info("[Redis清理] 删除响应记录 {} 条", responseKeys.size());
            }
            
            // 清理商家状态记录
            Set<String> statusKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.MERCHANT_STATUS_PREFIX + "*");
            if (statusKeys != null && !statusKeys.isEmpty()) {
                deletedCount += redisTemplate.delete(statusKeys).intValue();
                log.info("[Redis清理] 删除状态记录 {} 条", statusKeys.size());
            }
            
            return ResponseEntity.ok(Map.of(
                "success", true,
                "message", "Redis电力数据清理完成",
                "deletedCount", deletedCount
            ));
            
        } catch (Exception e) {
            log.error("[Redis清理] 清理失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "error", e.getMessage()
            ));
        }
    }
    
    /**
     * 查看当前Redis中的电力数据
     */
    @GetMapping("/power/keys")
    public ResponseEntity<Map<String, Object>> getPowerKeys() {
        try {
            Set<String> queryKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.POWER_QUERY_PREFIX + "*");
            Set<String> responseKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.POWER_RESPONSE_PREFIX + "*");
            Set<String> statusKeys = redisTemplate.keys(PowerDeviceConstants.RedisKeys.MERCHANT_STATUS_PREFIX + "*");
            
            return ResponseEntity.ok(Map.of(
                "success", true,
                "data", Map.of(
                    "queryKeys", queryKeys != null ? queryKeys : java.util.Collections.emptySet(),
                    "responseKeys", responseKeys != null ? responseKeys : java.util.Collections.emptySet(),
                    "statusKeys", statusKeys != null ? statusKeys : java.util.Collections.emptySet()
                )
            ));
            
        } catch (Exception e) {
            log.error("[Redis查询] 查询失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "error", e.getMessage()
            ));
        }
    }
}
